home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9206.ARJ / 1006024B < prev    next >
Text File  |  1992-06-02  |  2KB  |  72 lines

  1. //
  2. //    FAKELOCK.C
  3. //
  4. //
  5. //    This function simulates the type of objects that come with
  6. //    hardware locks.  A real lock object module would make a call to
  7. //    an external device attached to the printer port (or sometime the
  8. //    RS232 port).  The lock object module passes an input parameter to
  9. //    the external device and reads the return value created by the
  10. //    external device.  The lock object module then returns a value
  11. //    to the calling program.
  12. //
  13. //    FAKELOCK's input parameters are a pointer to a string of char
  14. //    and an int, telling which lock to use.  The second parameter is
  15. //    present to simulate two locks.
  16. //
  17.  
  18. #include <alloc.h>
  19. #include <string.h>
  20. #include "fakelock.h"
  21.  
  22. int     FAKELOCK( char* incode, int key )
  23. {
  24.   int   leng;            // length of input string, must be >=5
  25.   int   merg;            // place where incode is merged with itself
  26.   char* strincode;        // pointer to tempory storeage area
  27.   int*  strlong;        // points to place in string;
  28.   int  i,j;
  29.  
  30.   if ( (leng = strlen( incode )) < 5 )    // find leng of string parameter
  31.     return ( 0 );            // if not above minimun exit
  32.  
  33.   if ( (strincode = malloc( leng+4 )) == NULL )    // allocate tempory string area
  34.     return ( 0 );            // if tempory area not available exit
  35.  
  36.   strcpy( strincode, incode );        // copy input parameter to work area
  37.  
  38.   j = leng % 4;                // determine length to get even 4 bytes
  39.  
  40.   if ( j != 0 )                // if not even 4 bytes long
  41.   {
  42.     j = 4 - j;                // determine fill length
  43.     for ( i = 1; i <= j; i++ )
  44.     {
  45.       strincode[leng] = NULL;           // fill with NULL
  46.       leng++;                // increment length
  47.     }
  48.   }
  49.  
  50.   j = leng / 4;                // determine number of loops in merge
  51.   strlong = ((int *)strincode);        // get first four characters
  52.  
  53.   merg = *strlong;                   // move first part of string to merge
  54.  
  55.   for ( i = 1; i < j; i++ )
  56.   {
  57.     strlong++;                // increment to next part of in string
  58.     merg &= (long)(*strlong);        // bit wise and
  59.   }
  60.  
  61.   switch ( key )
  62.   {
  63.     case 1 : merg ^= 11239; break;    // special code for lock 1
  64.     case 2 : merg ^= 15096; break;    // special code for lock 2
  65.     default: merg  = 1;            // code when no key given
  66.   }
  67.  
  68.   free( strincode );
  69.  
  70.   return ( merg );
  71. }
  72.